package com.xoa.service.HSTmeeting.Impl;

import com.xoa.dao.HSTmeeting.HstMeetingMapper;
import com.xoa.dao.HSTmeeting.HstRoomMapper;
import com.xoa.dao.budget.BudgetLogMapper;
import com.xoa.dao.modulePriv.ModulePrivMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.HSTmeeting.HstMeetingWithBLOBs;
import com.xoa.model.HSTmeeting.HstRoomWithBLOBs;
import com.xoa.model.department.Department;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.users.UserPriv;
import com.xoa.model.users.Users;
import com.xoa.service.HSTmeeting.HstMeetRoomService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HstMeetRoomServiceImpl implements HstMeetRoomService {
    @Autowired
    HstRoomMapper hstRoomMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    ModulePrivMapper modulePrivMapper;
    @Autowired
    HstMeetingMapper hstMeetingMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private BudgetLogMapper budgetLogMapper;

    @Override
    public ToJson<HstRoomWithBLOBs> getAllHstMeetRoomInfo(Integer page, Integer pageSize, boolean useFlag) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
     /*   useFlag=true;
        page=1;
        pageSize=5;*/
        pageParams.setUseFlag(useFlag);

        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page", pageParams);
        List<HstRoomWithBLOBs> allHstMeetRoomInfo = null;
        try {
            allHstMeetRoomInfo = hstRoomMapper.getAllHstMeetRoomInfo(map);
            for (HstRoomWithBLOBs t : allHstMeetRoomInfo) {
                if (!StringUtils.checkNull(t.getRoomManager())) {
                    String[] splits = t.getRoomManager().split(",");
                    List<Users> usersByUids = usersMapper.getUsersByUserIds(splits);
                    StringBuffer sb = new StringBuffer();
                    for (Users u : usersByUids) {
                        sb.append(u.getUserName() + ",");
                    }
                    t.setRoomManagerName(sb.toString());
                }
                if (!StringUtils.checkNull(t.getUserIds())) {
                    String[] split = t.getUserIds().split(",");
                    List<Users> usersByUids = usersMapper.getUsersByUserIds(split);
                    StringBuffer sb = new StringBuffer();
                    for (Users u : usersByUids) {
                        sb.append(u.getUserName() + ",");
                    }
                    t.setUserIdsString(sb.toString());
                }
                // 部门
                if (!StringUtils.checkNull(t.getDeptIds())) {
                    String[] split = t.getDeptIds().split(",");
                    List<Department> deptNameByIds = modulePrivMapper.getDeptNameByIds(split);
                    StringBuffer sb = new StringBuffer();
                    for (Department d : deptNameByIds) {
                        sb.append(d.getDeptName() + ",");
                    }
                    t.setDeptIdsString(sb.toString());
                }
                if (!StringUtils.checkNull(t.getPrivIds())) {
                    String[] split = t.getPrivIds().split(",");
                    List<UserPriv> privNameByIds = modulePrivMapper.getPrivNameByIds(split);
                    StringBuffer sb = new StringBuffer();
                    for (UserPriv up : privNameByIds) {
                        sb.append(up.getPrivName() + ",");
                    }
                    t.setPrivIdsString(sb.toString());
                    int allHstMeetRoom = hstRoomMapper.getAllHstMeetRoom();
                    pageParams.setTotal(allHstMeetRoom);
                }
            }
            json.setObj(allHstMeetRoomInfo);
            json.setTotleNum(pageParams.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<HstRoomWithBLOBs> insertMeetRoomInfo(HstRoomWithBLOBs hstRoomWithBLOBs) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        try {
            if(StringUtils.checkNull(hstRoomWithBLOBs.getPrivIds())){
                hstRoomWithBLOBs.setPrivIds("");
            }
            if(StringUtils.checkNull(hstRoomWithBLOBs.getDeptIds())){
                hstRoomWithBLOBs.setDeptIds("");
            }
            if(StringUtils.checkNull(hstRoomWithBLOBs.getUserIds())){
                hstRoomWithBLOBs.setUserIds("");
            }
            int i = hstRoomMapper.insertSelective(hstRoomWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg("err");
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<HstRoomWithBLOBs> uodateMeetingRoom(HstRoomWithBLOBs hstRoomWithBLOBs) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        try {
            int i = hstRoomMapper.updateByPrimaryKeyWithBLOBs(hstRoomWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg("err");
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<HstRoomWithBLOBs> deleteMeetingRoom(String roomId) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        try {
            int i = hstRoomMapper.deleteByPrimaryKey(Integer.parseInt(roomId));
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg("err");
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<HstRoomWithBLOBs> getAllMeetRoomAll(Integer page, Integer pageSize, boolean useFlag) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        try {
            HstRoomWithBLOBs hstRoomWithBLOBs=new HstRoomWithBLOBs();
            hstRoomWithBLOBs.setDeptIds(String.valueOf(users.getDeptId()));
            hstRoomWithBLOBs.setUserIds(users.getUserId());
            hstRoomWithBLOBs.setPrivIds(String.valueOf(users.getUserPriv()));
            List<HstRoomWithBLOBs> allHstMeetRoomInfo = hstRoomMapper.getAllMeetRoomAll(hstRoomWithBLOBs);
            json.setObj(allHstMeetRoomInfo);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg("err");
            e.printStackTrace();
        }
        return json;
    }

    @Override
    public ToJson<HstRoomWithBLOBs> selectMeetRoomManager(Integer roomId) {
        ToJson<HstRoomWithBLOBs> json = new ToJson<HstRoomWithBLOBs>();
        try {
            HstRoomWithBLOBs hstRoomWithBLOBs = hstRoomMapper.selectMeetRoomManager(roomId);
            if (!StringUtils.checkNull(hstRoomWithBLOBs.getRoomManager())) {
                String[] split = hstRoomWithBLOBs.getRoomManager().split(",");
                List<Users> getManagerId = usersMapper.getUsersByUserIds(split);
                hstRoomWithBLOBs.setRoomManagerNames(getManagerId);
            }

            json.setObj1(hstRoomWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg("err");
            e.printStackTrace();
        }
        return json;
    }
    @Override
    public ToJson<HstMeetingWithBLOBs> myHstMeeting(HttpServletRequest request, HstMeetingWithBLOBs hstMeetingWithBLOBs, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<HstMeetingWithBLOBs> json = new ToJson<HstMeetingWithBLOBs>(1, "error");
        try{
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            Map<String, Object> map2 = new HashMap<String, Object>();
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
            hstMeetingWithBLOBs.setUserId(user.getUserId()+"");
            map.put("meetingWithBLOBs",hstMeetingWithBLOBs);
            map2.put("meetingWithBLOBs",hstMeetingWithBLOBs);
            //获取当前时间的时间戳，判断当前会议是否正在进行
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long currentTime=Long.valueOf(DateFormat.getTime(sdf.format(date)));
            List<HstMeetingWithBLOBs> meetingList3=hstMeetingMapper.selectAll2(map);
       /*     List<HstMeetingWithBLOBs> meetingList3=new ArrayList<>();
            for (HstMeetingWithBLOBs hstRoomWithBLOBs:meetingList){
                if (!StringUtils.checkNull(hstRoomWithBLOBs.getUserIds())){
                    String [] split= hstRoomWithBLOBs.getUserIds().split(",");
                    for (String s1 : split){
                        if (s1.equals(user.getUserId())){
                            meetingList3.add(hstRoomWithBLOBs);
                        }
                    }
                }
            }*/

            for(HstMeetingWithBLOBs meeting:meetingList3){
                if (!StringUtils.checkNull(meeting.getRoomId()+"")){
                    HstRoomWithBLOBs hstRoomWithBLOBs = hstRoomMapper.selectMeetRoomManager(meeting.getRoomId());

                    meeting.setHstRoomWithBLOBs(hstRoomWithBLOBs);

                    String[] split = meeting.getHstRoomWithBLOBs().getRoomManager().split(",");
                    List<Users> usersByUids = usersMapper.getUsersByUserIds(split);
                    StringBuffer sb = new StringBuffer();
                    for (Users u : usersByUids) {
                        sb.append(u.getUserName() + ",");
                    }
                    meeting.getHstRoomWithBLOBs().setRoomManagerName(sb.toString());
                }
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(meeting.getStartTime());
                Date startTime1=meeting.getStartTime();
                if (!StringUtils.checkNull(meeting.getAdvanceMin())&& "2".equals(meeting.getMeetStatus())){
                    long startTime2=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                    calendar.add(Calendar.MINUTE, Integer.parseInt(meeting.getAdvanceMin()));
                    meeting.setStartTime(calendar.getTime());
                    long startTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                    Calendar calendar2=Calendar.getInstance();
                    Date date2 = new Date();
                    calendar2.setTime(date2);
                    calendar2.add(Calendar.MINUTE, Integer.parseInt(meeting.getAdvanceMin()));
                    long currentTime2=Long.valueOf(DateFormat.getTime(sdf.format(calendar2.getTime())));
                    if(startTime2>=currentTime && currentTime2>=startTime2){
                        meeting.setAdvanceMinString("1");
                    }



                }
                 meeting.setStartTime(startTime1);
                meeting.setUserNameString(user.getUserName());
                if(!StringUtils.checkNull(meeting.getUserId())){
                    meeting.setUserIdName(usersMapper.getUsernameByUserId(meeting.getUserId()));
                }
                if(!StringUtils.checkNull(meeting.getManagerId())){
                    meeting.setManagerIdName(usersMapper.getUsernameByUserId(meeting.getManagerId()));
                }
                if(meeting.getRoomId() !=null && !meeting.getRoomId().equals(0) ){
                    HstRoomWithBLOBs hstRoomWithBLOBs = hstRoomMapper.selectMeetRoomManager(meeting.getRoomId());

                    meeting.setRoomName(hstRoomWithBLOBs.getRoomName());
                }
                /*else if (meeting.getMeetRoomId().equals(0)){
                    meeting.setMeetRoomName("视频会议");
                }*/

               /* if (!StringUtils.checkNull(meeting.getVideoConfFlag()) && meeting.getVideoConfFlag().equals("1")){
                    loginConf(meeting,user);
                }*/

                long startTime=0;
                long endTime=0;
                if(!StringUtils.checkNull(meeting.getStartTime()+"")){
                    //startTime= meeting.getStartTime().getTime();
                     startTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                }
                if(!StringUtils.checkNull(meeting.getEndTime()+"")){
                   // endTime= meeting.getEndTime().getTime();
                    endTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getEndTime())));

                }

                if(meeting.getMeetStatus()!=null){
                    switch(Integer.parseInt(meeting.getMeetStatus())){
                        case 1:
                            meeting.setMeetStatusString("待审批");
                            break;
                        case 2:
                            meeting.setMeetStatusString("已审批");
                            meeting.setMeetStatus("2");
                            /*if(startTime<=currentTime && currentTime<endTime){
                                meeting.setMeetStatusString("进行中");
                                meeting.setMeetStatus("3");
                            }else if(startTime<=currentTime && currentTime>=endTime){
                                meeting.setMeetStatusString("已结束");
                                meeting.setMeetStatus("5");
                            }else{
                                meeting.setMeetStatusString("已审批");
                                 meeting.setMeetStatus("2");
                            }
                            //对时间进行了判断，修改数据库，并更改返回值
                            int i = hstMeetingMapper.updHstMeetStatusById(meeting);
                            try {
                                if (i>0){
                                    BudgetLog log = new BudgetLog();
                                    log.setBudgetId(meeting.getMeetingId());
                                    log.setUserId("user.getUserId()");
                                    log.setTime(sdf.format(new Date()));// 现在时间
                                    try {
                                        log.setIp(sdf.format(meeting.getStartTime()));
                                    }catch (Exception e){

                                    }
                                    log.setType(String.valueOf(meeting.getMeetStatus()));
                                    log.setRemark("ip==开始时间，，input==结束时间");
                                    log.setBudgetItemName("548");
                                    try {
                                        log.setBudgetInputTime(meeting.getEndTime());
                                    }catch (Exception e){

                                    }
                                    budgetLogMapper.insertSelective(log);
                                }
                            }catch (Exception e){

                            }*/
                            break;
                        case 3:
                            meeting.setMeetStatusString("进行中");
                            meeting.setMeetStatus("3");
                            /*if(startTime<=currentTime && currentTime>=endTime){
                                meeting.setMeetStatusString("已结束");
                                meeting.setMeetStatus("5");
                            }else {
                                meeting.setMeetStatusString("进行中");
                                meeting.setMeetStatus("3");
                            }
                            int i1 = hstMeetingMapper.updHstMeetStatusById(meeting);
                            try {
                                if (i1>0){
                                    BudgetLog log = new BudgetLog();
                                    log.setBudgetId(meeting.getMeetingId());
                                    log.setUserId("user.getUserId()");
                                    log.setTime(sdf.format(new Date()));// 现在时间
                                    try {
                                        log.setIp(sdf.format(meeting.getStartTime()));
                                    }catch (Exception e){

                                    }
                                    log.setType(String.valueOf(meeting.getMeetStatus()));
                                    log.setRemark("ip==开始时间，，input==结束时间");
                                    log.setBudgetItemName("377");
                                    try {
                                        log.setBudgetInputTime(meeting.getEndTime());
                                    }catch (Exception e){

                                    }
                                    budgetLogMapper.insertSelective(log);
                                }
                            }catch (Exception e){

                            }
*/
                            break;
                        case 4:
                            meeting.setMeetStatusString("未批准");
                            break;
                        case 5:
                            meeting.setMeetStatusString("已结束");
                            break;
                    }
                }
                /*if(!StringUtils.checkNull(meeting.getStartTime())){
                    meeting.setStartTime(meeting.getStartTime().substring(0,meeting.getStartTime().length()-2));
                }
                if(!StringUtils.checkNull(meeting.getEndTime())){
                    meeting.setEndTime(meeting.getEndTime().substring(0,meeting.getEndTime().length()-2));
                }
                if(!StringUtils.checkNull(meeting.getCreateTime())){
                    meeting.setCreateTime(meeting.getCreateTime().substring(0,meeting.getCreateTime().length()-2));
                }*/
            }
            int i = hstMeetingMapper.selectCounts(map2);
            json.setObj(meetingList3);
            json.setFlag(0);
            json.setTotleNum(i);
            json.setMsg("ok");
        }catch (Exception e){
            json.setMsg(e.getMessage());
            L.e("MeetingServiceImpl getMyMeeting:"+e);
        }
        return json;
    }

    // 对于app公告  和 门户页面只查 2，3
    @Override
    public ToJson<HstMeetingWithBLOBs> myHstMeetings(HttpServletRequest request, HstMeetingWithBLOBs hstMeetingWithBLOBs, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<HstMeetingWithBLOBs> json = new ToJson<HstMeetingWithBLOBs>(1, "error");
        try{
            //分页
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            Map<String, Object> map2 = new HashMap<String, Object>();
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
            hstMeetingWithBLOBs.setUserId(user.getUserId()+"");
            map.put("meetingWithBLOBs",hstMeetingWithBLOBs);
            map2.put("meetingWithBLOBs",hstMeetingWithBLOBs);
            //获取当前时间的时间戳，判断当前会议是否正在进行
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long currentTime=Long.valueOf(DateFormat.getTime(sdf.format(date)));
            List<HstMeetingWithBLOBs> meetingList3=hstMeetingMapper.selectAll3(map);
       /*     List<HstMeetingWithBLOBs> meetingList3=new ArrayList<>();
            for (HstMeetingWithBLOBs hstRoomWithBLOBs:meetingList){
                if (!StringUtils.checkNull(hstRoomWithBLOBs.getUserIds())){
                    String [] split= hstRoomWithBLOBs.getUserIds().split(",");
                    for (String s1 : split){
                        if (s1.equals(user.getUserId())){
                            meetingList3.add(hstRoomWithBLOBs);
                        }
                    }
                }
            }*/

            for(HstMeetingWithBLOBs meeting:meetingList3){
                if (!StringUtils.checkNull(meeting.getRoomId()+"")){
                    HstRoomWithBLOBs hstRoomWithBLOBs = hstRoomMapper.selectMeetRoomManager(meeting.getRoomId());

                    meeting.setHstRoomWithBLOBs(hstRoomWithBLOBs);

                    String[] split = meeting.getHstRoomWithBLOBs().getRoomManager().split(",");
                    List<Users> usersByUids = usersMapper.getUsersByUserIds(split);
                    StringBuffer sb = new StringBuffer();
                    for (Users u : usersByUids) {
                        sb.append(u.getUserName() + ",");
                    }
                    meeting.getHstRoomWithBLOBs().setRoomManagerName(sb.toString());
                }
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(meeting.getStartTime());
                Date startTime1=meeting.getStartTime();
                if (!StringUtils.checkNull(meeting.getAdvanceMin())&& "2".equals(meeting.getMeetStatus())){
                    long startTime2=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                    calendar.add(Calendar.MINUTE, Integer.parseInt(meeting.getAdvanceMin()));
                    meeting.setStartTime(calendar.getTime());
                    long startTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                    Calendar calendar2=Calendar.getInstance();
                    Date date2 = new Date();
                    calendar2.setTime(date2);
                    calendar2.add(Calendar.MINUTE, Integer.parseInt(meeting.getAdvanceMin()));
                    long currentTime2=Long.valueOf(DateFormat.getTime(sdf.format(calendar2.getTime())));
                    if(startTime2>=currentTime && currentTime2>=startTime2){
                        meeting.setAdvanceMinString("1");
                    }



                }
                meeting.setStartTime(startTime1);
                meeting.setUserNameString(user.getUserName());
                if(!StringUtils.checkNull(meeting.getUserId())){
                    meeting.setUserIdName(usersMapper.getUsernameByUserId(meeting.getUserId()));
                }
                if(!StringUtils.checkNull(meeting.getManagerId())){
                    meeting.setManagerIdName(usersMapper.getUsernameByUserId(meeting.getManagerId()));
                }
                if(meeting.getRoomId() !=null && !meeting.getRoomId().equals(0) ){
                    HstRoomWithBLOBs hstRoomWithBLOBs = hstRoomMapper.selectMeetRoomManager(meeting.getRoomId());

                    meeting.setRoomName(hstRoomWithBLOBs.getRoomName());
                }
                /*else if (meeting.getMeetRoomId().equals(0)){
                    meeting.setMeetRoomName("视频会议");
                }*/

               /* if (!StringUtils.checkNull(meeting.getVideoConfFlag()) && meeting.getVideoConfFlag().equals("1")){
                    loginConf(meeting,user);
                }*/

                long startTime=0;
                long endTime=0;
                if(!StringUtils.checkNull(meeting.getStartTime()+"")){
                    //startTime= meeting.getStartTime().getTime();
                    startTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
                }
                if(!StringUtils.checkNull(meeting.getEndTime()+"")){
                    // endTime= meeting.getEndTime().getTime();
                    endTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getEndTime())));

                }

                if(meeting.getMeetStatus()!=null){
                    switch(Integer.parseInt(meeting.getMeetStatus())){
                        case 1:
                            meeting.setMeetStatusString("待审批");
                            break;
                        case 2:
                            meeting.setMeetStatusString("已审批");
                            meeting.setMeetStatus("2");
                            /*if(startTime<=currentTime && currentTime<endTime){
                                meeting.setMeetStatusString("进行中");
                                meeting.setMeetStatus("3");
                            }else if(startTime<=currentTime && currentTime>=endTime){
                                meeting.setMeetStatusString("已结束");
                                meeting.setMeetStatus("5");
                            }else{
                                meeting.setMeetStatusString("已审批");
                                meeting.setMeetStatus("2");
                            }
                            //对时间进行了判断，修改数据库，并更改返回值
                            int i = hstMeetingMapper.updHstMeetStatusById(meeting);
                            try {
                                if (i>0){
                                    BudgetLog log = new BudgetLog();
                                    log.setBudgetId(meeting.getMeetingId());
                                    log.setUserId("user.getUserId()");
                                    log.setTime(sdf.format(new Date()));// 现在时间
                                    try {
                                        log.setIp(sdf.format(meeting.getStartTime()));
                                    }catch (Exception e){

                                    }
                                    log.setType(String.valueOf(meeting.getMeetStatus()));
                                    log.setRemark("ip==开始时间，，input==结束时间");
                                    log.setBudgetItemName("548");
                                    try {
                                        log.setBudgetInputTime(meeting.getEndTime());
                                    }catch (Exception e){

                                    }
                                    budgetLogMapper.insertSelective(log);
                                }
                            }catch (Exception e){

                            }*/

                            break;
                        case 3:
                            meeting.setMeetStatusString("进行中");
                            meeting.setMeetStatus("3");
                            /*if(startTime<=currentTime && currentTime>=endTime){
                                meeting.setMeetStatusString("已结束");
                                meeting.setMeetStatus("5");
                            }else {
                                meeting.setMeetStatusString("进行中");
                                meeting.setMeetStatus("3");
                            }
                            int i1 = hstMeetingMapper.updHstMeetStatusById(meeting);
                            try {
                                if (i1>0){
                                    BudgetLog log = new BudgetLog();
                                    log.setBudgetId(meeting.getMeetingId());
                                    log.setUserId("user.getUserId()");
                                    log.setTime(sdf.format(new Date()));// 现在时间
                                    try {
                                        log.setIp(sdf.format(meeting.getStartTime()));
                                    }catch (Exception e){

                                    }
                                    log.setType(String.valueOf(meeting.getMeetStatus()));
                                    log.setRemark("ip==开始时间，，input==结束时间");
                                    log.setBudgetItemName("377");
                                    try {
                                        log.setBudgetInputTime(meeting.getEndTime());
                                    }catch (Exception e){

                                    }
                                    budgetLogMapper.insertSelective(log);
                                }
                            }catch (Exception e){

                            }*/
                            break;
                        case 4:
                            meeting.setMeetStatusString("未批准");
                            break;
                        case 5:
                            meeting.setMeetStatusString("已结束");
                            break;
                    }
                }
                /*if(!StringUtils.checkNull(meeting.getStartTime())){
                    meeting.setStartTime(meeting.getStartTime().substring(0,meeting.getStartTime().length()-2));
                }
                if(!StringUtils.checkNull(meeting.getEndTime())){
                    meeting.setEndTime(meeting.getEndTime().substring(0,meeting.getEndTime().length()-2));
                }
                if(!StringUtils.checkNull(meeting.getCreateTime())){
                    meeting.setCreateTime(meeting.getCreateTime().substring(0,meeting.getCreateTime().length()-2));
                }*/
            }
            int i = hstMeetingMapper.selectCounts(map2);
            json.setObj(meetingList3);
            json.setFlag(0);
            json.setTotleNum(i);
            json.setMsg("ok");
        }catch (Exception e){
            json.setMsg(e.getMessage());
            L.e("MeetingServiceImpl getMyMeeting:"+e);
        }
        return json;
    }

    @Override
    public ToJson<HstMeetingWithBLOBs> queryHstMeetingById(String meetId,HttpServletRequest request) {
        ToJson<HstMeetingWithBLOBs> json = new ToJson<HstMeetingWithBLOBs>(1, "error");
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            Map<String, Object> map = new HashMap<String, Object>();
            String userId = user.getUserId();//查询当前登录人的会议申请
            map.put("meetingId",meetId);
            HstMeetingWithBLOBs meeting = hstMeetingMapper.selectByPrimaryKey(Integer.parseInt(meetId));

            if (!StringUtils.checkNull(meeting.getUserId())) {
                meeting.setUserIdName(usersMapper.getUsernameByUserId(meeting.getUserId()));
            }
            if (!StringUtils.checkNull(String.valueOf(meeting.getRoomId()))){
                meeting.setRoomName(hstRoomMapper.selectByPrimaryKey(meeting.getRoomId()).getRoomName());
            }
            if (!StringUtils.checkNull(meeting.getUserIds())) {
                String[] split = meeting.getUserIds().split(",");
                List<Users> usersByUids = usersMapper.getUsersByUserIds(split);
                StringBuffer sb = new StringBuffer();
                for (Users u : usersByUids) {
                    sb.append(u.getUserName() + ",");
                }
                meeting.setUserIdsName(sb.toString());
            }
            if (!StringUtils.checkNull(meeting.getManagerId())) {
                String[] split = meeting.getManagerId().split(",");
                List<Users> getManagerId = usersMapper.getUsersByUserIds(split);
                StringBuffer sb = new StringBuffer();
                for (Users u : getManagerId) {
                    sb.append(u.getUserName() + ",");
                }
                meeting.setManagerIdName(sb.toString());
            }
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            attachmentList = GetAttachmentListUtil.returnAttachment(meeting.getAttachmentName(), meeting.getAttachmentId(),  "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_MEETING);
            meeting.setAttachmentList(attachmentList);
            //json.setObj(hstMeetingWithBLOBs);
            json.setObject(meeting);
            json.setFlag(0);
            json.setMsg("ok");
            // 修改会议状态
            meetingStatus2();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public void meetingStatus2(){
        List<HstMeetingWithBLOBs> hstMeetingWithBLOBs =hstMeetingMapper.selectAllApp();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        long currentTime=date.getTime();
        for (HstMeetingWithBLOBs meeting:hstMeetingWithBLOBs){
            long startTime=0;
            long endTime=0;
            if(null!=meeting.getStartTime()){
                startTime= meeting.getStartTime().getTime();
                //startTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getStartTime())));
            }
            if(null!=meeting.getEndTime()){
                endTime= meeting.getEndTime().getTime();
                //endTime=Long.valueOf(DateFormat.getTime(sdf.format(meeting.getEndTime())));

            }

            if(meeting.getMeetStatus()!=null){
                switch(Integer.parseInt(meeting.getMeetStatus())){
                    case 1:
                        meeting.setMeetStatusString("待审批");
                        break;
                    case 2:
                        if(startTime<=currentTime && currentTime<endTime){
                            meeting.setMeetStatusString("进行中");
                            meeting.setMeetStatus("3");
                        }else if(startTime<=currentTime && currentTime>=endTime){
                            meeting.setMeetStatusString("已结束");
                            meeting.setMeetStatus("5");
                        }else{
                            meeting.setMeetStatusString("已审批");
                            meeting.setMeetStatus("2");
                        }
                        //对时间进行了判断，修改数据库，并更改返回值
                        int i = hstMeetingMapper.updHstMeetStatusById(meeting);
                        /*try {
                            if (i>0&&!meeting.getMeetStatus().equals("2")){
                                BudgetLog log = new BudgetLog();
                                log.setBudgetId(meeting.getMeetingId());
                                log.setUserId(String.valueOf(currentTime));
                                log.setTime(sdf.format(new Date()));// 现在时间
                                try {
                                    log.setIp(sdf.format(meeting.getStartTime())+"////"+startTime);
                                }catch (Exception e){

                                }
                                log.setType(String.valueOf(meeting.getMeetStatus()));
                                log.setRemark("ip==开始时间，，input==结束时间"+"///"+736);
                                log.setBudgetItemName(sdf.format(meeting.getEndTime())+"////"+endTime);
                                try {
                                    //log.setBudgetInputTime(meeting.getEndTime());
                                }catch (Exception e){

                                }
                                budgetLogMapper.insertSelective(log);
                            }
                        }catch (Exception e){

                        }*/
                        break;
                    case 3:
                        meeting.setMeetStatusString("进行中");
                        meeting.setMeetStatus("3");
                        if(startTime<=currentTime && currentTime>=endTime){
                            meeting.setMeetStatusString("已结束");
                            meeting.setMeetStatus("5");
                        }else {
                            meeting.setMeetStatusString("进行中");
                            meeting.setMeetStatus("3");
                        }
                        int i1 = hstMeetingMapper.updHstMeetStatusById(meeting);
                        /*try {
                            if (i1>0&&!meeting.getMeetStatus().equals("3")){
                                BudgetLog log = new BudgetLog();
                                log.setBudgetId(meeting.getMeetingId());
                                log.setUserId(String.valueOf(currentTime));
                                log.setTime(sdf.format(new Date()));// 现在时间
                                try {
                                    log.setIp(sdf.format(meeting.getStartTime())+"////"+startTime);
                                }catch (Exception e){

                                }
                                log.setType(String.valueOf(meeting.getMeetStatus()));
                                log.setRemark("ip==开始时间，，input==结束时间"+"///"+772);
                                log.setBudgetItemName(sdf.format(meeting.getEndTime())+"////"+endTime);
                                try {
                                    //log.setBudgetInputTime(meeting.getEndTime());
                                }catch (Exception e){

                                }
                                budgetLogMapper.insertSelective(log);
                            }
                        }catch (Exception e){

                        }
*/
                        break;
                    case 4:
                        meeting.setMeetStatusString("未批准");
                        break;
                    case 5:
                        meeting.setMeetStatusString("已结束");
                        break;
                }
            }
        }
    }
}
